# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=libsquish
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=1.15
pkgrel=4
pkgdesc="Open source DXT compression library (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://libsquish.sourceforge.io/'
license=('MIT')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         "${MINGW_PACKAGE_PREFIX}-omp")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-libpng")
source=(https://sourceforge.net/projects/libsquish/files/${_realname}-${pkgver}.tgz
        0001-fix-install.patch)
sha256sums=('628796eeba608866183a61d080d46967c9dda6723bc0a3ec52324c85d2147269'
            '7ec7f612ac18a3dbade20e9ed1d7de36ad0abbec59c8147a6e5144fa03728dc7')
noextract=(${_realname}-${pkgver}.tgz)

prepare() {
  mkdir -p ${_realname}-${pkgver}
  tar -xzf "${srcdir}"/${_realname}-${pkgver}.tgz -C "${srcdir}"/${_realname}-${pkgver}
  cd "${srcdir}"/${_realname}-${pkgver}
  patch -p1 -i "${srcdir}"/0001-fix-install.patch
}

build() {
  mkdir -p "${srcdir}"/build-${MSYSTEM}-shared && cd "${srcdir}"/build-${MSYSTEM}-shared

  # Build shared version
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DBUILD_SHARED_LIBS=ON \
    -DBUILD_SQUISH_EXTRA=ON \
    ../${_realname}-${pkgver}
  ${MINGW_PREFIX}/bin/cmake --build .

  mkdir -p "${srcdir}"/build-${MSYSTEM}-static && cd "${srcdir}"/build-${MSYSTEM}-static
  # Build static version
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DBUILD_SHARED_LIBS=OFF \
    -DBUILD_SQUISH_EXTRA=ON \
    ../${_realname}-${pkgver}
  ${MINGW_PREFIX}/bin/cmake --build .
}

package() {
  cd "${srcdir}"/build-${MSYSTEM}-shared
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .

  cd "${srcdir}"/build-${MSYSTEM}-static
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .

  mkdir -p "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}
  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/LICENSE.txt "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE.txt

  mkdir -p "${pkgdir}"${MINGW_PREFIX}/lib/pkgconfig
  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/${_realname}.pc.in "${pkgdir}"${MINGW_PREFIX}/lib/pkgconfig/${_realname}.pc
  sed -s "s|@PREFIX@|${MINGW_PREFIX}|g" -i "${pkgdir}"${MINGW_PREFIX}/lib/pkgconfig/${_realname}.pc
  sed -s "s|@LIB_PATH@|lib|g" -i "${pkgdir}"${MINGW_PREFIX}/lib/pkgconfig/${_realname}.pc
}
